package controllers.administrator;

import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import services.AuctionService;
import services.BidService;
import controllers.AbstractController;
import domain.Auction;
import domain.Bid;

@Controller
@RequestMapping("/bid/administrator")
public class BidAdministratorController extends AbstractController {
	// Services
	@Autowired
	private BidService bidService;
	@Autowired
	private AuctionService auctionService;

	// Constructor
	public BidAdministratorController() {
		super();
	}

	// Listing methods
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ModelAndView list(@RequestParam int auctionId) {
		ModelAndView result;
		Collection<Bid> bids;
		Boolean isWinner;
		Auction auction;
		Integer idWinningBid;

		idWinningBid = null;
		auction = auctionService.findOne(auctionId);

		isWinner = auction.getWinningBid() != null;
		if (isWinner) {
			idWinningBid = auction.getWinningBid().getId();
		}

		bids = bidService.findBidsByAuctionIdByAdmin(auctionId);

		result = new ModelAndView("bid/list");
		result.addObject("requestURI", "bid/administrator/list.do");
		result.addObject("bids", bids);
		result.addObject("isWinner", isWinner);
		result.addObject("idWinningBid", idWinningBid);
		result.addObject("backURI", "/auction/administrator/finishedList.do");
		return result;
	}

}
